<?php

namespace app\api\controller;


use app\common\controller\Api;
use app\common\model\Address as ModelAddress;
use think\Db;
use Exception;


/**
 * 地址管理
 */
class Address extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    // protected $auth = ['test3'];

    /**
     * 创建地址
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="name", type="str", required=true, description="姓名")
     * @ApiParams (name="mobile", type="str", required=true, description="电话")
     * @ApiParams (name="citys", type="str", required=true, description="省市区-逗号分隔")
     * @ApiParams (name="address", type="str", required=true, description="详细地址")
     * @ApiParams (name="is_default", type="int", required=true, description="是否默认:0=否,1=是")
     * @ApiParams (name="lat", type="float", required=false, description="lat")
     * @ApiParams (name="lng", type="float", required=false, description="lng")
     * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function add()
    {

        $rej = $this->validate(
            $this->request->post(),
            [
                'name'  => 'require|max:25',
                'mobile'  => 'require|max:15',
                'citys' => 'require|max:255',
                'address' => 'require|max:255',
                'is_default'  => 'number',
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1006, [], $rej);
        }

        $user = $this->auth->getUser();
        $data = $this->request->post();
        $data['user_id'] = $user->id;
        $res = new ModelAddress($data);
        $res->save();
        $this->responseMsg(0);
    }



    /**
     * 编辑
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, description="地址ID")
     * @ApiParams (name="name", type="str", required=true, description="姓名")
     * @ApiParams (name="mobile", type="str", required=true, description="电话")
     * @ApiParams (name="citys", type="str", required=true, description="省市区-逗号分隔")
     * @ApiParams (name="address", type="str", required=true, description="详细地址")
     * @ApiParams (name="is_default", type="int", required=true, description="是否默认:0=否,1=是")
     * @ApiParams (name="lat", type="float", required=false, description="lat")
     * @ApiParams (name="lng", type="float", required=false, description="lng")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function edit()
    {
        $rej = $this->validate(
            $this->request->post(),
            [
                'id' => 'require|min:1',
                'name'  => 'require|max:25',
                'mobile'  => 'require|max:15',
                'citys' => 'require|max:255',
                'address' => 'require|max:255',
                'is_default'  => 'number',
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1006, [], $rej);
        }

        $res = ModelAddress::where('id', $this->request->post('id'))->find();
        if (!$res) {
            $this->responseMsg(1005, [], '未找到该地址');
        }
        $user = $this->auth->getUser();

        if ($res->user_id != $user->id) {
            $this->responseMsg(1006, [], '权限错误');
        }
        $data = $this->request->post();
        $res->name = $data['name'];
        $res->mobile = $data['mobile'];
        $res->citys = $data['citys'];
        $res->address = $data['address'];
        $res->is_default = $data['is_default'];
        $res->lat = $data['lat'];
        $res->lng = $data['lng'];
        $res->save();
        $this->responseMsg(0);
    }


    /**
     * 列表
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiReturnParams (name="code", type="integer", required=true, sample="0")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function list()
    {
        $user = $this->auth->getUser();
        $res = ModelAddress::where('user_id', $user->id)->order('id desc')->select();
        $this->responseMsg(0, $res);
    }


    /**
     * 详情
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, description="地址ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function detail()
    {
        $rej = $this->validate(
            $this->request->post(),
            [
                'id'  => 'require|number',
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        $user = $this->auth->getUser();

        $res = ModelAddress::get($this->request->post('id'));
        if (!$res) {
            $this->responseMsg(1005, [], '未找到该内容');
        }
        $this->responseMsg(0, $res);
    }

    /**
     * 删除地址
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, description="地址ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function del()
    {
        $rej = $this->validate(
            $this->request->post(),
            [
                'id'  => 'require|number|egt:1',
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        $user = $this->auth->getUser();

        $res = ModelAddress::get($this->request->post('id'));
        if (!$res) {
            $this->responseMsg(1005, [], '未找到该地址');
        }
        if ($res->user_id != $user->id) {
            $this->responseMsg(1005, [], '没得权限');
        }
        $res->delete();
        $this->responseMsg(0, '', '删除成功');
    }


    /**
     * 设置默认
     * @Author xy
     * @ApiTitle 
     * @ApiMethod (POST)
     * @ApiParams (name="id", type="integer", required=true, description="地址ID")
     * @ApiReturnParams (name="code", type="integer", required=true, description="")
     * @ApiReturn ({'code':0,'msg':'返回成功'})
     */
    public function setdefault()
    {
        $rej = $this->validate(
            $this->request->post(),
            [
                'id'  => 'require|number|egt:1',
            ]
        );
        if (true !== $rej) {
            $this->responseMsg(1005, [], $rej);
        }
        $user = $this->auth->getUser();

        $res = ModelAddress::get($this->request->post('id'));
        if (!$res) {
            $this->responseMsg(1005, [], '未找到该地址');
        }
        if ($res->user_id != $user->id) {
            $this->responseMsg(1005, [], '没得权限');
        }
        $res->is_default = 1;
        $res->save();
        $this->responseMsg(0);
    }
}
